<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增保单' : '编辑保单'"
    :visible.sync="visible"
    width="700px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="80px">

      <el-form-item v-if="type==='add'" label="客户编号" prop="orderCode">
        <el-input v-model="temp.orderCode" placeholder="请输入">
          <el-button slot="append" class="append-search">获取</el-button>
        </el-input>
      </el-form-item>
      <el-form-item v-else label="客户编号" prop="orderCode">
        <el-input v-model="temp.orderCode" placeholder="请输入" disabled />
      </el-form-item>
      <el-form-item label="客户名称" prop="csmName">
        <el-input v-model="temp.csmName" />
      </el-form-item>
      <el-form-item label="注册电话" prop="csmName">
        <el-input v-model="temp.csmName" />
      </el-form-item>

      <el-form-item label="客户类型" prop="csmPhone">
        <el-select v-model="temp.select" placeholder="请选择">
          <el-option v-for="(l,i) in cmsType" :key="i" :label="l.desc" :value="l.value" />
        </el-select>
      </el-form-item>

      <el-form-item style="width: 100%;" label="所在区域" prop="csmPhone">
        <el-select v-model="temp.selec1t" style="width: 20%;" placeholder="请选择">
          <el-option v-for="(l,i) in cmsType" :key="i" :label="l.desc" :value="l.value" />
        </el-select>
        <el-select v-model="temp.selec2t" style="width: 20%;" placeholder="请选择">
          <el-option v-for="(l,i) in cmsType" :key="i" :label="l.desc" :value="l.value" />
        </el-select>
        <el-select v-model="temp.sele3ct" style="width: 20%;" placeholder="请选择">
          <el-option v-for="(l,i) in cmsType" :key="i" :label="l.desc" :value="l.value" />
        </el-select>
        <el-select v-model="temp.sele4ct" style="width: 20%;" placeholder="请选择">
          <el-option v-for="(l,i) in cmsType" :key="i" :label="l.desc" :value="l.value" />
        </el-select>
      </el-form-item>
      <el-form-item style="width: 100%;" label="注册地址" prop="csmPhone">
        <el-input v-model="temp.csmName" style="width: 70%;" />
        <el-input v-model="temp.csmName" style="width: 10%;" /> 楼
      </el-form-item>
      <el-form-item label="身份证号" prop="orderCode">
        <el-input v-model="temp.orderCode" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="所属机构" prop="orgSNNo">
        <el-tree-select
          v-model="temp.orgSNNo"
          :clearable="false"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择组织机构',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'default-expand-all': true,
            'expand-on-click-node': false,
            'highlight-current': true,
            props:{
              value:'orgSNNo',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
          @node-click="selectTreeChange"
        />
      </el-form-item>

      <el-form-item label="客户等级" prop="address">
        <el-select v-model="temp.address" placeholder="请选择">
          <el-option v-for="(l,i) in cmsLevel" :key="i" :value="l.value" :label="l.desc" />
        </el-select>
      </el-form-item>
      <el-form-item label="销售人员" prop="price">
        <el-input v-model="temp.price" clearable />
      </el-form-item>
      <el-form-item label="执照编号" prop="price">
        <el-input v-model="temp.price" clearable />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { } from '@/api'

  export default {
    name: 'add',
    props: ['treeData', 'cmsLevel', 'cmsType'],
    data() {
      return {
        visible: false,
        type: 'add',
        temp: {
          select: 1,
          csmPhone: '',
          orderCode: '',
          csmName: '',
          address: '',
          addScores: '',
          price: '',
          time: '',
          remark: ''
        },
        rules: {
          // csmPhone: [{ required: true, message: '请输入', trigger: 'change' }],
          // csmName: [{ required: true, message: '请输入用户编码', trigger: 'change' }],
          // orderCode: [{ required: true, message: '请输入押金单号', trigger: 'change' }],
          // address: [{ required: true, message: '请选择投保地址', trigger: 'change' }],
          // price: [{ required: true, message: '请输入投保金额', trigger: 'change' }]
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
            this.type = 'edit'
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          if (this.type === 'add') {
            this.add()
          } else {
            this.edit()
          }
        })
      },
      async add() {
        // await roleAdd(this.temp)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit() {
        // await roleUpdate(this.temp)
        this.resolve(this.temp)
        this.visible = false
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      selectTreeChange(value) {
        this.orgSNNo = value.orgSNNo
      },
      reset() {
        this.type = 'add'
        this.temp = {
          select: 1,
          csmPhone: '',
          orderCode: '',
          csmName: '',
          address: '',
          addScores: '',
          price: '',
          time: '',
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
.info-title {
  display: flex;
  align-items: center;
  margin-top: 10px;
  margin-bottom: 20px;
  .title-icon {
    display: inline-block;
    width: 4px;
    height: 18px;
    margin-right: 6px;
    background: $themeColor;
  }
  span {
    font-weight: 700;
    color: $themeColor;
  }
}
</style>
